package com.jinlejia.erp.ui.house.prospect;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.prospect.UploadHouseTypeImageAdapter;
import com.jinlejia.erp.databinding.ActivityProspectChooseHouseTypeBinding;

import java.util.List;

/**
 * 选择户型图
 */
public class ProspectChooseHouseTypeActivity extends AppCompatActivity {
    private UploadHouseTypeImageAdapter houseTypeImageAdapter;
    private Helper helper;

    private String rsCode, houseType, rsType,houseUsage;
    private int usage;
    private ActivityProspectChooseHouseTypeBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_prospect_choose_house_type);

        binding.toolbarBackImageView.setOnClickListener(v->toolBarFinish());
        binding.rightButton.setOnClickListener(v->saveAndFinish());
        helper = Helper.init(this);
        helper.message().closeMessage();
        Bundle extras = getIntent().getExtras();
        if (extras==null)throw new NullPointerException();
        this.rsType = extras.getString(Type.PARAM.HOUSE_SOURCE_TYPE);
        this.rsCode = extras.getString(Type.PARAM.HOUSE_SOURCE_CODE);
        this.houseType = extras.getString(Type.PARAM.HOUSE_TYPE);
        this.houseUsage = extras.getString(Type.PARAM.HOUSE_USAGE); //房屋用途
        this.usage = extras.getInt(Type.PARAM.HOUSE_USAGE_TYPE,-1);
        binding.toolbarTitleTextView.setText("选择户型图");
        houseTypeImageAdapter = new UploadHouseTypeImageAdapter(this);
        if ("null".equalsIgnoreCase(this.houseType) || TextUtils.isEmpty(this.houseType)){
            String usageType = helper.message().getUsageNameByUsageType(usage);
            binding.houseTypeHeaderView.setLabelName(usageType);
            showMessage(null);
            initView(null);
        }else {
            Helper.Util.HouseInfo info = helper.util().formatHouseInfo(this.houseType);
            if (info==null){
                String usageType = helper.message().getUsageNameByUsageType(usage);
                binding.houseTypeHeaderView.setLabelName(usageType);
                showMessage(null);
                initView(null);
            }else {
                String tHouseType = info.getRoomCount()+"室";
                binding.houseTypeHeaderView.setLabelName(tHouseType);
                showMessage(info);
                initView(info);
            }

        }
    }

    private void showMessage(Helper.Util.HouseInfo info){
        if (info==null){
            helper.message().showHouseInfo(usage);
        }else {
            helper.message().showHouseInfo(usage,info);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==20 && resultCode == Activity.RESULT_OK){
            int position = data.getIntExtra("selectPosition", -1);
            if (position!=-1){
                houseTypeImageAdapter.select(position);
                if (data.getBooleanExtra("save",false)){
                    saveAndFinish(); //当从预览页已经选择时，则直接关闭本页，并且返回相关数据
                }
            }else {
                houseTypeImageAdapter.select(-1);
            }
        }
    }

    private void initView(final Helper.Util.HouseInfo houseInfo) {
        String houseType;
        if (houseInfo==null){
            houseType = "";
        }else {
            houseType = houseInfo.toSpecialText(); //1室
        }

        houseTypeImageAdapter.setListener((selectedPosition, position, sources) -> {
            Intent intent = new Intent();
            intent.setClass(ProspectChooseHouseTypeActivity.this,ProspectViewPictureActivity.class);
            intent.putExtra(ProspectViewPictureActivity.PARAM_POSITION,position);
            intent.putExtra(ProspectViewPictureActivity.PARAM_URLS,sources);
            intent.putExtra(ProspectViewPictureActivity.PARAM_SELECT_POSITION,selectedPosition);
            startActivityForResult(intent,20);
        });
        helper.http().postListApartment(rsCode, houseType, new Helper.Http.ListApartmentCallBack() {
            @Override
            public void onResponse(List<UploadHouseTypeImageAdapter.HouseTypeSource> list) {
                if (list.isEmpty()){
                    binding.emptyLayout.setVisibility(View.VISIBLE);
                    return;
                }else {
                    binding.emptyLayout.setVisibility(View.GONE);
                }
                houseTypeImageAdapter.addAllSource(list);
                binding.houseTypeGridView.setAdapter(houseTypeImageAdapter);

            }

            @Override
            public void onEmpty() {
                binding.emptyLayout.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void finish() {
        setResult(RESULT_CANCELED);
        super.finish();
    }

    public void toolBarFinish() {
        this.finish();
    }

    public void saveAndFinish(){
        UploadHouseTypeImageAdapter.HouseTypeSource source = houseTypeImageAdapter.getSelectSource();
        if (source == null) {
            setResult(RESULT_CANCELED);
        } else {
            Intent intent = new Intent();
            intent.putExtra("data", source);
            setResult(RESULT_OK, intent);
        }
        super.finish();
    }


    @Override
    protected void onDestroy() {
        helper.release();
        super.onDestroy();
    }
}
